package com.ruoyi.web.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.common.BusinessException;
import com.ruoyi.web.entity.AccountInfo;
import com.ruoyi.web.mapper.AccountInfoMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <br>
 *
 * @author chenwujie
 * @date 2021/2/11
 */
@Service
public class AccountInfoService extends ServiceImpl<AccountInfoMapper, AccountInfo> {


    public AccountInfo getCurrentAccount() {
        LambdaQueryWrapper<AccountInfo> param = new QueryWrapper<AccountInfo>()
                .lambda()
                .orderByDesc(AccountInfo::getDataDate)
                .last("limit 0,1");
        return this.baseMapper.selectOne(param);
    }

    public AccountInfo getByDate(LocalDate localDate) {
        LambdaQueryWrapper<AccountInfo> param = new QueryWrapper<AccountInfo>()
                .lambda()
                .eq(AccountInfo::getDataDate, localDate);
        return this.baseMapper.selectOne(param);
    }


    public void updateAccountSubtract(BigDecimal amount) {
        LocalDate today = LocalDate.now();
        LocalDateTime now = LocalDateTime.now();

        AccountInfo currentAccount = getCurrentAccount();

        if (today.equals(currentAccount.getDataDate())) {
            currentAccount.setLiquidity(currentAccount.getLiquidity().subtract(amount));
            if(currentAccount.getLiquidity().compareTo(BigDecimal.ZERO)<0){
                throw new BusinessException("更新后流动资金为{0}",currentAccount.getLiquidity().toPlainString());
            }

            currentAccount.setUpdateTime(now);
            this.saveOrUpdate(currentAccount);
        } else {
            AccountInfo accountInfo = new AccountInfo();
            accountInfo.setDataDate(today);
            accountInfo.setLiquidity(currentAccount.getLiquidity().subtract(amount));
            if(currentAccount.getLiquidity().compareTo(BigDecimal.ZERO)<0){
                throw new BusinessException("更新后流动资金为{0}",currentAccount.getLiquidity().toPlainString());
            }
            accountInfo.setCreateTime(now);
            accountInfo.setUpdateTime(now);
            this.saveOrUpdate(accountInfo);
        }
    }

    public void updateAccountAdd(BigDecimal amount) {
        LocalDate today = LocalDate.now();
        LocalDateTime now = LocalDateTime.now();

        AccountInfo currentAccount = getCurrentAccount();

        if (today.equals(currentAccount.getDataDate())) {
            currentAccount.setLiquidity(currentAccount.getLiquidity().add(amount));
            currentAccount.setUpdateTime(now);
            this.saveOrUpdate(currentAccount);
        } else {
            AccountInfo accountInfo = new AccountInfo();
            accountInfo.setDataDate(today);
            accountInfo.setLiquidity(currentAccount.getLiquidity().add(amount));
            accountInfo.setCreateTime(now);
            accountInfo.setUpdateTime(now);
            this.saveOrUpdate(accountInfo);
        }
    }


}